package com.dd.cloud.user.util.ad;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;

/**
 * @author Lx
 * @version 1.0
 * @date 2023/12/25
 */
public class DESCUtils {
    /**
     * 填充方式
     */
    private static final String algorithm = "DES/ECB/PKCS5Padding";

    /**
     * 加密
     *
     * @param plainText 待加密明文
     * @param desKey    密钥
     * @return
     * @throws Exception
     */
    public static byte[] encrypt(byte[] plainText, String desKey) throws Exception {
        Cipher cipher = createCipher(Cipher.ENCRYPT_MODE, desKey);
        byte[] encryptedData = cipher.doFinal(plainText);
        return encryptedData;
    }

    /**
     * 解密
     *
     * @param plainText
     * @param desKey
     * @return
     * @throws Exception
     */
    public static byte[] decrypt(byte[] plainText, String desKey) throws Exception {
        Cipher cipher = createCipher(Cipher.DECRYPT_MODE, desKey);
        byte[] decryptedData = cipher.doFinal(plainText);
        return decryptedData;
    }

    /**
     * 解密
     *
     * @param plainText
     * @param desKey
     * @return String
     * @throws Exception
     */
    public static String decryptToString(byte[] plainText, String desKey) throws Exception {
        byte[] decryptedData = decrypt(plainText, desKey);
        String decryptedStringData = new String(decryptedData, StandardCharsets.UTF_8);
        return decryptedStringData;
    }

    /**
     * 创建Cipher
     *
     * @param decryptMode 模式（1：加密、2：解密）
     * @param desKey      密钥（string）
     * @return
     * @throws Exception
     */
    public static Cipher createCipher(int decryptMode, String desKey) throws Exception {
        byte[] rawKeyData = desKey.getBytes();
        DESKeySpec dks = new DESKeySpec(rawKeyData);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        javax.crypto.SecretKey key = keyFactory.generateSecret(dks);
        SecureRandom secureRandom = new SecureRandom();
        Cipher cipher = Cipher.getInstance(algorithm);
        cipher.init(decryptMode, key, secureRandom);
        return cipher;
    }

    /**
     * 将二进制转换成16进制
     *
     * @param buf
     * @return
     */
    public static String parseByte2HexStr(byte[] buf) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < buf.length; i++) {
            String hex = Integer.toHexString(buf[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }

    /**
     * 将16进制转换为二进制
     *
     * @param hexStr
     * @return
     */
    public static byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1) return null;
        byte[] result = new byte[hexStr.length() / 2];
        for (int i = 0; i < hexStr.length() / 2; i++) {
            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }
}
